package com.ms.p2p.controller.admin;

import com.alibaba.dubbo.config.annotation.Reference;
import com.google.common.collect.Lists;
import com.ms.common.bean.Message;
import com.ms.common.exception.NoticeException;
import com.ms.p2p.bean.MenuItem;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.entity.system.AdminLog;
import com.ms.p2p.entity.system.RbacRole;
import com.ms.p2p.provider.AdminLogProvider;
import com.ms.p2p.provider.RbacProvider;
import com.ms.p2p.provider.SettingProvider;
import com.ms.p2p.utils.HttpTookit;
import com.ms.p2p.utils.JsonDataGridHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created by xing_ on 2017/5/27.
 */
@Controller("adminIndexController")
@RequestMapping("/admin")
public class AdminIndexContrller extends AdminBaseController {

    @Reference
    private AdminLogProvider adminLogService;
    @Reference
    private RbacProvider rbacProvider;
    @Reference
    private SettingProvider settingProvider;
    @Reference
    AdminLogProvider loginLogService;

    /**
     * 获取登录充值记录
     * @param request
     * @param session
     * @param page
     * @return
     */
    @RequestMapping(value = {"/queryData"}, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public PageBean queryData(HttpServletRequest request, HttpSession session
            , @RequestParam(value = "type",required = false) String type
            , @RequestParam(value = "begin_date",required = false) String begin_date
            , @RequestParam(value = "end_date",required = false) String end_date
            ,PageBean page
    ) {
        page.setList(adminLogService.findPager(type,begin_date,end_date,page));
        page.setTotalCount(adminLogService.findCount(type,begin_date,end_date));
        return JsonDataGridHelper.createJSONData(page);
    }

    @RequestMapping({"/celarCache"})
    @ResponseBody
    public Message celarCache(Model model)  {
        settingProvider.clearCache();
        return Message.success("清空系统缓存成功");
    }


    @RequestMapping("/index/main")
    public String main() {

        return "admin/index";

    }

    @RequestMapping(value="/welcome", method= RequestMethod.GET)
    public String welcome(Model model) {

        return "/admin/common/welcome";
    }
    /**
     * 顶部导航菜单
     * @return
     */
    @RequestMapping("/menu/nav")
    @ResponseBody
    public List nav() {
        return rbacProvider.findDeptRolesByPid(getCurrentEmp().getDepart(),"0");
    }


    @RequestMapping("/menu/getLeftMenu")
    @ResponseBody
    public List<RbacRole> getLeftMenu() {
        List<MenuItem> menus = Lists.newArrayList();
        int dept_id = getCurrentEmp().getDepart();
        List<RbacRole> list = rbacProvider.findDeptRolesByPid(dept_id, "0");
        List <RbacRole> list3 = new ArrayList<RbacRole>();
       // list3.addAll(list);
        for(RbacRole role : list){
            ArrayList<RbacRole> list2 = (ArrayList<RbacRole>) rbacProvider.findDeptRolesByPid(dept_id, role.getId() + "");
            list3.addAll(list2);
            for(RbacRole role1 : list2){
                ArrayList<RbacRole> list4 = (ArrayList<RbacRole>) rbacProvider.findDeptRolesByPid(dept_id, role1.getId() + "");
                list3.addAll(list4);
                for(RbacRole role2 : list4){
                    ArrayList<RbacRole> list5 = (ArrayList<RbacRole>) rbacProvider.findDeptRolesByPid(dept_id, role2.getId() + "");
                    list3.addAll(list5);
                }
            }


        }
        return list3;
    }

    /**
     * 获取子菜单
     * @param pid
     * @return
     */
    @RequestMapping("/menu/m/{pid}")
    @ResponseBody
    public List<MenuItem> menus(@PathVariable("pid") String pid) {
        List<MenuItem> menus = Lists.newArrayList();
        int dept_id = getCurrentEmp().getDepart();
        List<RbacRole> list = rbacProvider.findDeptRolesByPid(dept_id, pid);
        for(RbacRole role : list){
            MenuItem menuItem = new MenuItem(role.getName(), role.getId()+"",role.getUrl(),role.getPid()+"");
            ArrayList<RbacRole> list2 = (ArrayList<RbacRole>) rbacProvider.findDeptRolesByPid(dept_id, role.getId() + "");
            if(list2== null || list2.size() == 0){
               menus.add(menuItem);
               continue;
            }
            menuItem.setCollapsed(false);
            List<MenuItem> childern = Lists.newArrayList();
            for(RbacRole child : list2){
                childern.add(new MenuItem(child));
            }
            menuItem.setItems(childern);
            menus.add(menuItem);
        }
        return menus;
    }


    /**
     * 管理员登录日志
     * @param request
     * @return
     */
    @RequestMapping({ "/log/list" })
    public String userAccounts(HttpServletRequest request) {

        String begin_date = request.getParameter("begin_date");
        String end_date = request.getParameter("end_date");
        String type = request.getParameter("type");
        PageBean page = new PageBean();

        String currentPage = request.getParameter("currentPage");
        if (currentPage != null && !(("").equals(currentPage))) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        List<AdminLog> data = adminLogService.findPager(type,begin_date,end_date,page);
        page.setTotalCount(adminLogService.findCount(type,begin_date,end_date));
        request.setAttribute("page",page);
        request.setAttribute("datas",data);
        request.setAttribute("type",type);
        request.setAttribute("begin_date",begin_date);
        request.setAttribute("end_date",end_date);
        return "admin/system/log_list";
    }

    @Override
    public boolean getBinderFlag() {
        return true;
    }
}
